package cn.lzy;

import cn.lzy.taskmanagement.email.SendEmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


/**
 * @Author: WeiKaiYe
 * @CreateTime: 2023-10-25  09:53
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class SendEmailTestApplication {
    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private TemplateEngine templateEngine;


    // 纯文本邮件
    @Test
    public void sendSimpleEmailTest() {
        String to = "3115549579@qq.com";
        String subject = "【纯文本邮件】标题";
        String text = "Spring Boot纯文本邮件发送内容测试.....";
        // 发送简单邮件
        sendEmailService.sendSimpleEmail(to, subject, text);
    }


    // 附件邮件
    @Test
    public void sendAttachFileEmailTest(){
        String filePath="D:\\website\\Test\\狂人日记.docx";                   // 欢迎阅读狂人日记！
        sendEmailService.sendAttachFileEmail("3115549579@qq.com","【附件邮件】标题"," ",filePath);
    }


    // 图片邮件
    @Test
    public void sendImageEmail() {
        String to="3115549579@qq.com";
        String subject="【图片邮件】标题";
        // 定义邮件内容
        StringBuilder text = new StringBuilder();
        text.append("<html><head></head>");
        text.append("<body>");
        // cid为固定写法，rscId指定一个唯一标识
        String rscId = "img001";
        text.append("<img src='cid:" +rscId+"'/></body>");
        text.append("</html>");
        // 指定静态资源文件和附件路径
        String rscPath="D:\\website\\Test\\白雪公主.jpg";
        // 发送邮件
        sendEmailService.sendImageEmail(to,subject,text.toString(),rscId,rscPath);
    }


    //发送附件、图片、文本邮件    复杂邮件
    @Test
    public void sendComplexEmailTest() {
        String to="3115549579@qq.com";
        String subject="【复杂邮件】标题";
        // 定义邮件内容
        StringBuilder text = new StringBuilder();
        text.append("<html><head></head>");
        text.append("<body><h1>HelloWord 你好，白雪公主...</h1>");
        // cid为固定写法，rscId指定一个唯一标识
        String rscId = "img001";
        text.append("<img src='cid:" +rscId+"'/></body>");
        text.append("</html>");
        // 指定静态资源文件和附件路径
        String rscPath="D:\\website\\Test\\白雪公主.jpg";
        String filePath="D:\\website\\Test\\狂人日记.docx";
        // 发送复杂邮件
        sendEmailService.sendComplexEmail(to,subject,text.toString(),filePath,rscId,rscPath);
    }


    // 发送模板邮件
    @Test
    public void sendTemplateEmailTest(){
        String to="3115549579@qq.com";
        String subject="【模板邮件】标题";
        // 使用模板邮件定制邮件正文内容
        Context context =new Context();

        context.setVariable("username","朱丽叶");
        context.setVariable("code","888666");
        // 使用TemplateEngine 设置要处理的模板页面
        String emailContent = templateEngine.process("emailTemplate_vercode",context);
        // 发送模板邮件
        sendEmailService.sendTemplateEmail(to,subject,emailContent);
    }

}
